package com.example.skytakeoutserver

import org.springframework.http.server.reactive.HttpHandler
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter
import reactor.core.publisher.Mono
import reactor.netty.http.server.HttpServer

/**
 * todo: Optional to run ?
 */
class FluxMainApplication

fun main(args: Array<String>) {
    val handler = HttpHandler { request, response ->
        val uri = request.uri.toString()
        println(Thread.currentThread().toString() + "请求进来: " + uri)

        val factory = response.bufferFactory()
        val dataBuffer = factory.wrap(("$uri Hello World!").toByteArray())

        val body = Mono.just(dataBuffer)

        response.writeWith(body)
    }

    val adapter = ReactorHttpHandlerAdapter(handler)

    HttpServer.create()
        .host("localhost")
        .port(8080)
        .handle(adapter)
        .bindNow()

    println("服务器启动完毕, 监听8080")
    readln()
    println("服务器停止")
}